package com.ruoyi.app.service.impl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.app.aspect.PermissionAspect;
import com.ruoyi.app.domain.ArticleInfo;
import com.ruoyi.app.domain.ArticleInfoExample;
import com.ruoyi.app.domain.TripInfo;
import com.ruoyi.app.dto.LoginUserDto;
import com.ruoyi.app.mapper.ArticleInfoMapper;
import com.ruoyi.app.mapper.TripInfoMapper;
import com.ruoyi.app.service.PermissionService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/*
@description:
@ClassName PermissionServiceImpl
@author chen
@create 2025-03-14 15:23
@Version 1.0
*/
@Slf4j
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService
{
    final ArticleInfoMapper articleInfoMapper;
    final TripInfoMapper tripInfoMapper;

    /*
     * @==============History===============<br/>;
     * @Description //   发布文章
     * @Date 2025/3/18
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public AjaxResult insertArticleInfo(ArticleInfo articleInfo)
    {
        articleInfo.setDisable(false);
        articleInfo.setCreateTime(new Date());
        articleInfo.setUpdateTime(new Date());
        LoginUserDto loginUserDto = PermissionAspect.loginUser.get();
        articleInfo.setCreateId(loginUserDto.getId());
        articleInfo.setUpdateId(loginUserDto.getId());
        List<String> articlePictureList = articleInfo.getArticlePictureList();
        if(null != articlePictureList && articlePictureList.size() > 0)
        {
            String result = String.join(",", articlePictureList);
            articleInfo.setArticlePicture(result);
        }
        articleInfo.setTemp1(IpUtils.getIpAddress(ServletUtils.getRequest()));// 省份
        return articleInfoMapper.insertSelective(articleInfo) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /*
     * @==============History===============<br/>;
     * @Description //   更新文章
     * @Date 2025/3/18
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public AjaxResult updateArticleInfo(ArticleInfo articleInfo)
    {
        articleInfo.setUpdateTime(new Date());
        LoginUserDto loginUserDto = PermissionAspect.loginUser.get();
        articleInfo.setUpdateId(loginUserDto.getId());
        articleInfo.setTemp1(IpUtils.getIpAddress(ServletUtils.getRequest()));
        List<String> articlePictureList = articleInfo.getArticlePictureList();
        if(null != articlePictureList && articlePictureList.size() > 0)
        {
            String result = String.join(",", articlePictureList);
            articleInfo.setArticlePicture(result);
        }
        return articleInfoMapper.updateByPrimaryKeySelective(articleInfo) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /*
     * @==============History===============<br/>
     * @Description //   用户删除文章
     * @Date 2025-3-18 19:28
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @Override
    public AjaxResult deleteArticleInfo(Long id)
    {
        ArticleInfoExample articleInfoExample = new ArticleInfoExample();
        articleInfoExample.createCriteria().andIdEqualTo(id);
        return articleInfoMapper.deleteByExample(articleInfoExample) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /*
     * @==============History===============<br/>;
     * @Description //   用户发表行程
     * @Date 2025/3/20
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public AjaxResult insertTripInfo(TripInfo tripInfo)
    {
        tripInfo.setCreateTime(new Date());
        tripInfo.setDisable(false);
        tripInfo.setUpdateTime(new Date());
        LoginUserDto loginUserDto = PermissionAspect.loginUser.get();
        tripInfo.setCreateId(loginUserDto.getId());
        tripInfo.setUpdateId(loginUserDto.getId());
        tripInfo.setTemp1(IpUtils.getIpAddress(ServletUtils.getRequest()));
        tripInfo.setTemp2(IpUtils.isaAbroad(ServletUtils.getRequest()));// 1国内,0国外
        List<String> tripPictureList = tripInfo.getTripPictureList();
        if(null != tripPictureList && tripPictureList.size() > 0)
        {
            String result = String.join(",", tripPictureList);
            tripInfo.setTripPicture(result);
        }
        JSONObject temp3Obj = tripInfo.getTemp3Obj();
        if(null != temp3Obj)
        {
            tripInfo.setTemp3(JSON.toJSONString(temp3Obj));
        }
        JSONObject tripContentObj = tripInfo.getTripContentObj();
        if(null != tripContentObj)
        {
            tripInfo.setTripContent(JSON.toJSONString(tripContentObj));
        }

        return tripInfoMapper.insertSelective(tripInfo) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult updateTripInfo(TripInfo tripInfo)
    {
        tripInfo.setUpdateTime(new Date());
        LoginUserDto loginUserDto = PermissionAspect.loginUser.get();
        tripInfo.setUpdateId(loginUserDto.getId());
        tripInfo.setTemp2(IpUtils.isaAbroad(ServletUtils.getRequest()));
        tripInfo.setTemp1(IpUtils.getIpAddress(ServletUtils.getRequest()));
        JSONObject temp3Obj = tripInfo.getTemp3Obj();
        if(null != temp3Obj)
        {
            tripInfo.setTemp3(JSON.toJSONString(temp3Obj));
        }
        JSONObject tripContentObj = tripInfo.getTripContentObj();
        if(null != tripContentObj)
        {
            tripInfo.setTripContent(JSON.toJSONString(tripContentObj));
        }
        List<String> tripPictureList = tripInfo.getTripPictureList();
        if(null != tripPictureList && tripPictureList.size() > 0)
        {
            String result = String.join(",", tripPictureList);
            tripInfo.setTripPicture(result);
        }
        return tripInfoMapper.updateByPrimaryKeySelective(tripInfo) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult deleteTripInfo(Long id)
    {
        return tripInfoMapper.deleteByPrimaryKey(id) > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
